﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using QuanLyDoiXe.lop_doi_tuong;
using QuanLyDoiXe.Lop_du_lieu;
using System.IO;
using BarcodeLib;

namespace QuanLyDoiXe
{
    public partial class frmtkdiadiemtiepnhan : Form
    {
        SqlConnection conn = new SqlConnection("Data Source=ACER;Initial Catalog=quanlydoixevinamilk;Integrated Security=True");
        public frmtkdiadiemtiepnhan()
        {
            InitializeComponent();
        }

        private void btnthoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        #region 1. tim kiem 1
        private void btntimkiem1_Click(object sender, EventArgs e)
        {
            foreach (ListViewItem t in lvtkdiadiem.Items)
            {
                t.BackColor = Color.White;
                t.ForeColor = Color.Black;
            }
            foreach (ListViewItem t in lvtkdiadiem.Items)
            {
                for (int i = 0; i < lvtkdiadiem.Items.Count; i++)
                {
                    if (t.SubItems[2].Text.ToLower().StartsWith((cbbtkloaidiadiem.Text.ToLower())))
                    {
                        t.BackColor = Color.Blue;
                        t.ForeColor = Color.White;
                        break;
                    }
                }
            }
        }
        string tmp = "";
        ListViewItem[] listItem = new ListViewItem[0];
        private void cbbtkloaidiadiem_KeyPress(object sender, KeyPressEventArgs e)
        {
            try
            {
                tmp = cbbtkloaidiadiem.Text + e.KeyChar;
                if (lvtkdiadiem.Items.Count > 0)
                {
                    foreach (ListViewItem t in lvtkdiadiem.Items)
                    {
                        if (!t.SubItems[2].Text.ToLower().StartsWith(tmp.ToLower()))
                        {
                            Array.Resize(ref listItem, listItem.Length + 1);
                            lvtkdiadiem.Items.Remove(t);
                            listItem[listItem.Length - 1] = t;
                            Array.Resize(ref listItem, listItem.Length + 1);
                            lvtkdiadiem.Items.Remove(t);
                            listItem[listItem.Length - 1] = t;
                        }
                    }
                }

                if (listItem.Length > 0)
                {
                    for (int i = 0; i < listItem.Length; i++)
                    {
                        if (listItem[i].SubItems[2].Text.ToLower().StartsWith(tmp.ToLower()))
                        {
                           lvtkdiadiem.Items.Add(listItem[i]);
                            Array.Copy(listItem, i + 1, listItem, i, listItem.Length - i - 1);
                        }
                    }
                }
            }
            catch (Exception ex)
            { }
        }

        private void cbbtkloaidiadiem_TextChanged(object sender, EventArgs e)
        {
            if (cbbtkloaidiadiem.Text == "")
            {
                HienThiDiaDiem();
            }
        }
        #endregion

        #region 2. tim kiem 2
        private void btntimkiem_Click(object sender, EventArgs e)
        {
            foreach (ListViewItem t in lvtkdiadiem.Items)
            {
                t.BackColor = Color.White;
                t.ForeColor = Color.Blue;
            }
            foreach (ListViewItem t in lvtkdiadiem.Items)
            {
                for (int i = 0; i < lvtkdiadiem.Items.Count; i++)
                {
                    if (t.SubItems[1].Text.ToLower().StartsWith(txttktendiadiem.Text.ToLower()) && txttktoado.Text == "")
                    {
                        t.BackColor = Color.Blue;
                        t.ForeColor = Color.White;
                        break;
                    }
                    else
                        if (t.SubItems[1].Text.ToLower().StartsWith((txttktendiadiem.Text.ToLower())) && t.SubItems[4].Text.ToLower().StartsWith(txttktoado.Text.ToLower()))
                        {
                            t.BackColor = Color.Blue;
                            t.ForeColor = Color.White;
                            break;
                        }
                }
            }
        }
        string tm = "";
        ListViewItem[] listItem1 = new ListViewItem[0];
        private void txttktendiadiem_KeyPress(object sender, KeyPressEventArgs e)
        {
            try
            {
                tm = txttktendiadiem.Text + e.KeyChar;
                if (lvtkdiadiem.Items.Count > 0)
                {
                    foreach (ListViewItem t in lvtkdiadiem.Items)
                    {
                        if (!t.SubItems[1].Text.ToLower().StartsWith(tmp.ToLower()))
                        {
                            Array.Resize(ref listItem1, listItem.Length + 1);
                            lvtkdiadiem.Items.Remove(t);
                            listItem[listItem.Length - 1] = t;
                            Array.Resize(ref listItem, listItem.Length + 1);
                            lvtkdiadiem.Items.Remove(t);
                            listItem[listItem.Length - 1] = t;
                        }
                    }
                }

                if (listItem1.Length > 0)
                {
                    for (int i = 0; i < listItem1.Length; i++)
                    {
                        if (listItem1[i].SubItems[2].Text.ToLower().StartsWith(tmp.ToLower()))
                        {
                            lvtkdiadiem.Items.Add(listItem1[i]);
                            Array.Copy(listItem1, i + 1, listItem1, i, listItem1.Length - i - 1);
                        }
                    }
                }
            }
            catch (Exception ex)
            { }
        }

        private void txttktendiadiem_TextChanged(object sender, EventArgs e)
        {
            if (txttktendiadiem.Text == "")
            {
                HienThiDiaDiem();
            }
        }
        #endregion

        #region 3. xu ly
        ListViewItem item;
        private void HienThiDiaDiem()
        {
            try
            {
                conn.Open();
                SqlCommand cmd = new SqlCommand("Select * from diadiem,loaiDD where diadiem.MloaiDD=loaiDD.MloaiDD ", conn);
                SqlDataReader sqlDA = cmd.ExecuteReader();
                int i = 0;
                lvtkdiadiem.View = View.Details;
                lvtkdiadiem.FullRowSelect = true;
                while (sqlDA.Read())
                {
                    i++;
                    item = new ListViewItem(i.ToString());
                    item.SubItems.Add(sqlDA["Tendiadiem"].ToString());
                    item.SubItems.Add(sqlDA["Tenloai"].ToString());
                    item.SubItems.Add(sqlDA["Diachi"].ToString());
                    item.SubItems.Add(sqlDA["Toado"].ToString());
                    item.SubItems.Add(sqlDA["SDT"].ToString());
                    lvtkdiadiem.Items.Add(item);
                }
                cmd.Dispose();
                sqlDA.Close();
                conn.Close();
            }
            catch
            {
            }
        }

        private void lvtkdiadiem_SelectedIndexChanged(object sender, EventArgs e)
        {
            foreach (ListViewItem t in lvtkdiadiem.Items)
            {
                t.BackColor = Color.White;
                t.ForeColor = Color.Black;
            }
        }
        private void frmtkdiadiemtiepnhan_Load(object sender, EventArgs e)
        {
            cbbtkloaidiadiem.Focus();
            txttktendiadiem.Focus();
            txttktoado.Focus();
            HienThiDiaDiem();
        }
        #endregion
    }
}
